var member;
$(function(){
	//获取cookie中的对象
	if(member){
		$("#mId").val(member.mId);
	}else{
		window.location.href=rootPath+"/member/loginPre?redirect=/order/orderData?orderId="+orderId;
	}
	//请求订单信息
   oneOrder();


   // 管理收货地址
   $('.orderAddress').click(function(){
	//	location.href=rootPath + '/address/maAdress';
   });

	//取消删除订单
	$(document).on("click",".footer  .cacelOrdelOrder",function(){//由于a标签是动态生成的,onclick事件传入的参数无法
		debugger;
		var $orderId = $(this).attr("orderid");
		var $status = $(this).attr("status");		
		//alert("$status: " + $status +" $orderId: "+$orderId);
		cancelOrDeleteOrder($orderId,$status);
	});
	
	//确认收货
	$(document).on("click",".footer  .confirmReceiptOrder",function(){//由于a标签是动态生成的,onclick事件传入的参数无法
		var $orderId = $(this).attr("orderid");
		confirmReceiptOrder($orderId);
	});

});
//订单详细
function oneOrder(){
	var url = rootPath + "/order/oneOrder"; 
	var result = CommnUtil.ajax(url,{"orderId":orderId},"json");
	var order = null; //订单信息
	var products = null; //商品集合
	if(result.resultSuccess == true){
		order = result.data;
		if(order){
			products = order.products;
		}
		var orderCardsPayUrl = rootPath + "/order/order_card_pay?orderId="+orderId; //订单追加评价
//		var expressUrl ='https://m.kuaidi100.com/index_all.html?type='+order.expressName+'&postid=' + order.expressCode;
		var expressUrl = rootPath +'/member/information?expressName='+order.expressName+'&expressCode=' + order.expressCode+'&orderId='+order.orderId+'&newsTitle=物流信息';
//		var delOrderUrl = rootPath + "/order/cancelOrDeleteOrder?status=1&orderId="+orderId; //删除订单 status 0:取消订单 ;1：删除订单

		var status='';
        var $progressStr ="";
		//设置订单号
		$(".orderDa > p > span:last ").text(orderId);
		var payStatus = order.payStatus;
		var statusDetail='';
		var payStatus = order.payStatus;
		var shipStatus = order.shipStatus;
		var isComment = order.isComment;
		var isValid = order.isValid;

		var pmtOrder =order.pmtOrder;//订单优惠
		var proType =order.proType;//优惠方式
		var pmtGoods =order.pmtGoods;
	    var proDetailMap =order.proDetailMap;//店铺促销活动
		var memo=order.memo?order.memo:'暂无备注';
		var $infoStr='<div>'
			            +'<img src="'+rootPath+'/image/dw.png" alt="" class="left"/>'
		                +'<p class="clearfix">'
						     +'<span class="fl">收货人：'+ order.memberName +'</span>'
							 +'<span class="fr">'+ order.memberPhone+ '</span>'
						+'</p>'
						+'<p class="oad">收货地址：' + order.shipAddress+'</p>'		
					+'</div>';
		var  $pStr='<div>'
			        +'<p class="beizhu">买家备注:'+memo+'</p>'			
					+'</div>';
	     $(".classNote").html($pStr);

		pmtGoods =(pmtGoods==null)?0:pmtGoods;
		pmtOrder =(pmtOrder==null)?0:pmtOrder;
		$(".orderAddress").html($infoStr);  //配送信息
		$(".shoppingcart_store a span").text(order.shopName); //店铺名称
		
		if(order.payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='0'){
			$(".welcome").hide();
			statusDetail ='等待买家付款';
			status='<a class="fr cacelOrdelOrder" href="javascript:void(0)" orderId="'+orderId+'" status="0"   target="_self">取消订单</a><a class="fr" href="'+orderCardsPayUrl+'" target="_blank" >付款</a>';
			
		}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0' && isComment=='0' && isValid=='0'){
			$(".welcome").show();
			statusDetail ='等待卖家发货';	
			status='<!--<a class="fr" href="" target="_blank">提醒发货</a>-->';
						
		}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')&& isComment=='0' && isValid=='0'){
			$(".welcome").show();
			statusDetail ='卖家已发货';			
			status='<a class="fr" href="'+expressUrl+'" target="_self">查看物流</a><br /><a class="fr confirmReceiptOrder" href="javascript:void(0)"   orderId="'+orderId+'" target="_blank">确认收货</a>';
			
		}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='0' && isValid=='0'){
			$(".welcome").show();
			statusDetail ='交易成功';
			status='<a class="fr cacelOrdelOrder" orderId="'+orderId+'" status="1"   target="_self">删除订单</a><br /><a class="fr" href="" target="_self">查看物流</a>'
		//	+'<a class="fr"  href="'+commentUrl+'" target="_blank">评价</a>';
		}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='1' && isValid=='0'){
			$(".welcome").show();
			statusDetail ='交易成功';			
			status='<a class="fr cacelOrdelOrder"  orderId="'+orderId+'" status="1" target="_self">删除订单</a>'
			      +'<a class="fr" href="'+expressUrl+'" target="_self">查看物流</a>'
		//	      +'<a class="fr  href="'+addCommentUrl+'" target="_blank">追加评价</a>';
		
		}else if(payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='1'){
			$(".welcome").show();
			statusDetail ='交易关闭';
			status='<a class="fr cacelOrdelOrder" href="javascript:;"   orderId="'+orderId+'" status="1" target="_self">删除订单</a>';
			
		}
		$(".orderDa .spanW").text(statusDetail); //订单状态
		if(order.payType){
			 //1:畅捷通快捷银联支付 2.畅捷通微信扫码支付 3：畅捷通支付宝扫码支付 4.畅捷通微信app支付 5：畅捷通支付宝app支付 6：微信公众号支付 7：微信app支付 8：其它支付
			 if("1" == order.payType){
				$(".payWay").text("畅捷通快捷银联支付")                   
			 }else if("2" == order.payType){
				$(".payWay").text("畅捷通微信扫码支付")  
			 }else if("3" == order.payType){
				$(".payWay").text("畅捷通支付宝扫码支付")  
			 }else if("4" == order.payType){
				$(".payWay").text("畅捷通微信app支付")  
			 }else if("5" == order.payType){
				$(".payWay").text("畅捷通支付宝app支付 ")  
			 }else if("6" == order.payType){
				$(".payWay").text("微信公众号支付")  
			 }else if("7" == order.payType){
					$(".payWay").text("其它支付")
			 }
		}
		
		if(order.expressName)
		  $(".shipWay").text(order.expressName);//快递
		$.each(products,function(i,product){
			  var commentUrl = rootPath + "/order/evaluate?orderId="+orderId+"&goodsId="+product.productId; //订单评价
			  var addCommentUrl = rootPath + "/order/addEvaluate?orderId="+orderId+"&goodsId="+product.productId; //订单追加评价
			  var productDetailUrl = rootPath + "/goods/queryGoodsDetail?id=" + product.productId;
			  var goods_sku_info = '';
				if(product.goods_sku_info){
					goods_sku_info = product.goods_sku_info;
				}else{
					goods_sku_info ='无规格信息';
				}
			  var goodsSkus = goods_sku_info.split(",");
			   var skusStr = '';			   
			   $.each(goodsSkus,function(i,goodsSku){
				   if(goodsSku){
					   skusStr +=  '<span>'+ goodsSku +'</span>'  
				   }
             	});
			   var $unitPrice ="";
				if(product.discountMarketPrice){
					$unitPrice = product.discountMarketPrice;
				}else{
					$unitPrice = product.marketPrice;
				}
				var goodsPro = goodsProMoney(totalPrice,product.goodsProMap);//商品促销优惠	
                var $proDetail = '无优惠';
                if(!CommnUtil.notNull(goodsPro)){
                	$proDetail = "参与了" + goodsPro.proDetailTitle+'优惠活动，优惠了'+ goodsPro.discountamount.toFixed(2) +'元';
                }

        		var picture  = rootPath+"/image/pic5.png";
        		if(product.picture){
        			picture = product.picture;
        		}
        		var unitPrice=parseFloat($unitPrice);
			   var $product ='<div class="shoppingcartDetail"><a class="sdImg" href="'+ productDetailUrl +'">'
				               +'<img src="' + picture + '" alt="' + product.productName + '" class="sis"/>'
			                   +'<p class="sdWordA moreWord">' + product.productName + '</p>'
								 +'<p class="sdWordC">'
									//+'<span>颜色: <i>天蓝色</i></span>'
									//+'<span>尺码: <b>165/s</b></span>'
								    +skusStr
								 +'</p>'
								+'<p class="sdWordP "><b>￥</b><i>'+unitPrice.toFixed(2)+'</i></p>'
								+'<span class="mWord">x'+product.quantity+'</span>'
						    +'</a>'
						    
						  //判断订单详细中商品评论状态
			        		if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && product.isComment=='0' && isValid=='0'){
			        			$product +='<a class="comment" href="'+commentUrl+'" target="_blank" > 评价</a>';
			        		}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && product.isComment=='1' && isValid=='0'){
		        				if(product.isCommentCount <2 ){
		        					$product +='<a class="comment" href="'+addCommentUrl+'" target="_blank">追加评价</a>';
		        				}
			        		}
			   				$product +='</div>';
			    $(".shoppingcart_goods > li").append($product);
			
		});	
	    var totalPrice = parseFloat(order.totalAmount) - parseFloat(order.shipMoney);
	    $(".totalAmount").text(totalPrice.toFixed(2));//商品总额
	    $(".expressFee").text(order.shipMoney);
	    var price01=parseFloat(order.totalAmount);
	    $(".totalN .price > i").text(price01.toFixed(2));

		
	    $(".totalB p:nth-child(1) > i").text(orderId); 
	    $(".totalB").append('<p>订单编号:<i>' + orderId + '</i></p>');
	    if(order.transactionId){
	        $(".totalB").append('<p>交易单号:<i>' + order.transactionId + '</i></p>');
	    }	
		if(order.orderCreatTime){
		    $(".totalB").append('<p>创建时间:<i>' + order.orderCreatTime + '</i></p>');
		}
		if(order.orderPayTime){
		    $(".totalB").append('<p>付款时间:<i>' + order.orderPayTime + '</i></p>');
		}
		if(order.orderShipTime){
		    $(".totalB").append('<p>发货时间:<i>' + order.orderShipTime + '</i></p>');
		}
		if(order.orderOverTime){
		    $(".totalB").append('<p>结束时间:<i>' + order.orderOverTime + '</i></p>');
		}
	
		if( CommnUtil.notNull(proDetailMap.proDetailTitle)){
			$(".proActivety").text(proDetailMap.proDetailTitle);
			$(".proMoney").text(pmtOrder);
		}else{
			$(".proMoney").text(pmtGoods);
		}
		$(".orderStatus").html(status);
	}else if(result.resultSuccess == false){
		alert("查询订单信息失败!")
	}
}
//商品促销优惠	
function goodsProMoney(totalPrice,goodsProMap){
	var result={};
	if(CommnUtil.notNull(goodsProMap)){
		var proType = goodsProMap.proType;
		if('0' == proType || '1' == proType ){ //订单满减或 商品满减
			result["proDetailTitle"] = goodsProMap.proDetailTitle;
			result["discountamount"] = goodsProMap.discountamount;
	    }else if('2' == proType){//商品满赠	
			result["proDetailTitle"] = goodsProMap.proDetailTitle;	
		}else if('3' == proType){//商品打折
			result["proDetailTitle"] = goodsProMap.proDetailTitle;
			 var $discountRate = goodsProMap.discountRate;
			 result["discountamount"]  = totalPrice*(1-$discountRate*1);		
		}else{
			return result;
		}
	}
	return result;
}



//刷新订单列表
function reloadOrderList(){
	//window.location.href=rootPath + '/order/orderPage?queryStatus=' + queryStatus;
	window.location.href=rootPath + '/order/orderData?orderId=' + orderId;
}
//删除或取消
function cancelOrDeleteOrder($orderId,$status){ //订单取消订单 status 0:取消订单 ;1：删除订单
	var url =  rootPath + "/order/cancelOrDeleteOrder";
	var confirmInfo = $status == 0 ?"确认要取消订单吗？":"确认要删除订单吗？";
  var alarmInfoSuccess = $status == 0 ?"订单取消成功!":"订单删除成功!";
  var alarmInfoFail = $status == 0 ?"订单取消失败!":"订单删除失败!";
		if(confirm(confirmInfo)){

			var result = CommnUtil.ajax(url,
										{"orderId":$orderId,
									    "status":$status},
										"json");
			if(result.resultSuccess){
				 alert(alarmInfoSuccess);	
		         Pause(this,6000);//调用暂停函数
			     this.NextStep=function(){
                    window.location.href=rootPath + '/order/myOrders?queryStatus=0';
			     }
		}else{
			 alert(alarmInfoFail);	
	         Pause(this,6000);//调用暂停函数
		     this.NextStep=function(){
		    	 reloadOrderList()
		     }
		}
	}else{
			return false;
	}
}

//确认收货
function  confirmReceiptOrder($orderId){
	var url =  rootPath + "/order/confirmReceiptOrder";
    if(confirm("要确认收货吗？")){
		var result = CommnUtil.ajax(url,
				{"orderId":$orderId},
				"json");
		if (result.resultSuccess) {
			alert("确认收货成功");
			Pause(this, 6000);// 调用暂停函数
			this.NextStep = function() {
				reloadOrderList()
			}
		} else {
			alert("确认收货失败");
			Pause(this, 6000);//调用暂停函数
			this.NextStep = function() {
				reloadOrderList()
			}
		}
	}else{
			return false;
	}
}